function Pager(args) {
    var self = this;
    self.currentPage = ko.observable(args.params.pageNumber || 1);
    self.pageCount = ko.observable(0);
    self.pagesToShow = ko.observable([]);
    self.itemsPerPage = args.itemsPerPage;
    self.totalItems = ko.observable(0);
    self.goToFirstVisible = ko.observable(false);
    self.goToLastVisible = ko.observable(false);
    self.searchString = "";
    self.chosenType = -1;
    self.goTopPx = args.params.topPx;
    self.changeUrl = args.params.changeUrl;

    self.getPageSuccess = function (page) {
        self.currentPage(page.pageIndex);
        self.totalItems(page.totalItems);
        self.pageCount(page.totalPages);
        self.setPagesToShow();
        
        if(self.changeUrl) {
        	changeHashValue("page", page.pageIndex);
        }
        
        if(self.goTopPx >= 0) {
	        $('html,body').animate({
	        	scrollTop: self.goTopPx
	        }, 500);
        }
        args.onSuccess(page);
    };

    self.getPage = function (pageNumber) {
    	args.params.pageNumber = pageNumber;
    	getService(args.webService, args.params, self.getPageSuccess);
    };

    self.getNextPage = function () {
        self.currentPage(self.currentPage() + 1);
        self.getPage(self.currentPage());
    };

    self.getPreviousPage = function () {
        self.currentPage(self.currentPage() - 1);
        self.getPage(self.currentPage());
    };
    
    self.getFirstPage = function () {
    	self.getPage(1);
    };
    
    self.getLastPage = function () {
    	self.getPage(self.pageCount());
    };

    self.setSearchString = function (str) {
        self.searchString = str;
    };

    self.setChosenType = function (type) {
        self.chosenType = type;
    };

    self.setPagesToShow = function () {
        var pages = [];
        var firstPage;
        var lastPage;

        if (self.pageCount() < 8) {
            firstPage = 1;
            lastPage = self.pageCount();
        } else if (this.currentPage() < 4) {
            firstPage = 1;
            lastPage = 7;
        } else if (self.currentPage() > self.pageCount() - 3) {
            lastPage = self.pageCount();
            firstPage = lastPage - 6;
        } else {
            firstPage = self.currentPage() - 3;
            lastPage = self.currentPage() + 3;
        }

        for (var i = firstPage; i <= lastPage; i++) {
            pages.push(i);
        }

        self.goToFirstVisible(firstPage > 1 ? true : false);
        self.goToLastVisible(lastPage < self.pageCount() ? true : false);
        self.pagesToShow(pages);
    };

    self.isFirstPage = ko.computed(function () { return (self.currentPage() == 1); });
    self.moreItems = ko.computed(function () { return (self.currentPage() < self.pageCount()); });
    self.showPagingNumbers = ko.computed(function () { return (self.pageCount() != 1); });

    self.getPage(self.currentPage());
}